/**
 * 
 */
package net.oauth.core.parameters;

import java.util.Map;
import java.util.Map.Entry;

import net.oauth.util.OAuth1Util;
import net.oauth.util.ParameterPairer;

/**
 * @author Buhake Sindi
 * @since 07 March 2012
 *
 */
public abstract class OAuthParameterPairer extends ParameterPairer<String> {

	private boolean oauth1Encode;

	/**
	 * @param startDelimiter
	 * @param endDelimiter
	 * @param pairDelimiter
	 * @param oauth1Encode
	 */
	protected OAuthParameterPairer(String startDelimiter, String endDelimiter,
			String pairDelimiter, boolean oauth1Encode) {
		super(startDelimiter, endDelimiter, pairDelimiter);
		this.oauth1Encode = oauth1Encode;
	}

	/* (non-Javadoc)
	 * @see net.oauth.util.ParameterPairer#toString(java.util.Map)
	 */
	@Override
	public String toString(Map<String, String> attributeMap) {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		for (Entry<String, String> entry : attributeMap.entrySet()) {
			if (sb.length() > 0) {
				sb.append(pairDelimiter);
			}
			
			String key = oauth1Encode ? OAuth1Util.encode(entry.getKey()) : entry.getKey();
			String value = oauth1Encode ? OAuth1Util.encode(entry.getValue()) : entry.getValue();			
			sb.append(key).append(startDelimiter).append(value).append(endDelimiter);
		}
		
		return sb.toString();
	}
}
